<?php
namespace App\Http\Controllers\Admin;

use App\Models\Feedback;
use App\Models\Group;
use App\Models\Performance;
use App\Models\SystemSetting;
use App\Models\User;
use App\Models\RewardPunish;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Input;


class UserController extends BaseController
{


    public function __construct() {
        parent::__construct();
        $this->middleware('init');
    }


    public function getIndex(Request $request)
    {
        $users = User::where('id','!=','1')->orderBy('created_at')->paginate(20);

        return view('admin.kaohe.users.user')
            ->with('users', $users);
    }


    public function getUserAdd()
    {
        $group = Group::all();
        return view("admin.kaohe.users.add")
            ->with('group',$group)
            ->with('blademode','add');
    }


    public function getUserEdit($id)
    {
        $user = User::find($id);
        if(empty($user)){
            abort(404);
        }
        $group = Group::all();
        return view("admin.kaohe.users.add")
            ->with('group',$group)
            ->with('user',$user)
            ->with('blademode','edit');
    }

    //奖励详细列表
    public function getReward(Request $request)
    {
        $users = RewardPunish::where('category','=','1')->orderBy('created_at')->paginate(20);

        return view('admin.kaohe.users.reward')
            ->with('users', $users);
    }

    //惩罚详细列表
    public function getPunish(Request $request)
    {
        $users = RewardPunish::where('category','=','2')->orderBy('created_at')->paginate(20);

        return view('admin.kaohe.users.punish')
            ->with('users', $users);
    }



    public function postUserStore(Request $request)
    {
        $this->validate($request, [
            'name'          => 'required|max:255',
        ]);
        $role_id = $request->input('role_id','0');
        $user_id = $request->input('user_id','0');
        if($role_id > 0){//新建管理员
            if($user_id > 0){
                $user = User::find($user_id);
                if(empty($user)){
                    abort(404);
                }
                $user->role_id = $role_id;
                $user->name = $request->input('name','1');
                $user->password = bcrypt('cimers123');
                $user->is_active = $request->input('is_active','0');
                $user->save();
                return Response::json(array('id'=>$user->id,'status'=>'success','msg'=>'用户数据保存成功！','redirectUrl'=>'/admin/user'));
            }else{
                $user = new User();
                $user->role_id = $role_id;
                $user->name = $request->input('name','1');
                $user->password = bcrypt('cimers123');
                $user->is_active = $request->input('is_active','0');
                $user->save();
                return Response::json(array('id'=>$user->id,'status'=>'success','msg'=>'用户数据保存成功！','redirectUrl'=>'/admin/user'));
            }

        }else{//新建普通用户
            $data = $request->only('name','role_id','is_leader','group_id');
            if($user_id > 0){
                $user = User::find($user_id);
                if(empty($user)){
                    abort(404);
                }
                //检测是否满足存储条件
                if($data['is_leader'] == 1){
                    if($user_id > 0){
                        $exist = User::where('group_id',$data['group_id'])->where('status','1')->where('is_leader','1')->where('user_id','!=',$user_id)->first();
                    }
                    else{
                        $exist = User::where('group_id',$data['group_id'])->where('status','1')->where('is_leader','1')->first();
                    }
                    if(!empty($exist)){
                        return Response::json(array('id'=>$user->id,'status'=>'fail','msg'=>'该组已经有组长，请核实修改！'));
                    }
                }elseif($data['is_leader'] == 0){
                    $group = Group::find($data['group_id']);
                    /*if($group->class_id != 3){
                        return Response::json(array('id'=>$user->id,'status'=>'fail','msg'=>'分组必须为最下层分组，请重新选择'));
                    }*/
                }
                $user->name = trim($data['name']);
                $user->role_id = trim($data['role_id']);
                $user->password = bcrypt('cimers123');
                $user->is_leader = trim($data['is_leader']);
                $user->group_id = trim($data['group_id']);
                $user->save();
            }else{
                $user = new User();
                //检测是否满足存储条件
                if($data['is_leader'] == 1){
                    if($user_id > 0){
                        $exist = User::where('group_id',$data['group_id'])->where('status','1')->where('is_leader','1')->where('user_id','!=',$user_id)->first();
                    }
                    else{
                        $exist = User::where('group_id',$data['group_id'])->where('status','1')->where('is_leader','1')->first();
                    }
                    if(!empty($exist)){
                        return Response::json(array('id'=>$user->id,'status'=>'fail','msg'=>'该组已经有组长，请核实修改！'));
                    }
                }elseif($data['is_leader'] == 0){
                    $group = Group::find($data['group_id']);
                    /*if($group->class_id != 3){
                        return Response::json(array('id'=>$user->id,'status'=>'fail','msg'=>'分组必须为最下层分组，请重新选择'));
                    }*/
                }
                $user->name = trim($data['name']);
                $user->role_id = trim($data['role_id']);
                $user->password = bcrypt('cimers123');
                $user->is_leader = trim($data['is_leader']);
                $user->group_id = trim($data['group_id']);
                $user->save();
                $performance = new Performance();
                $performance->user_id = $user->id;
                $performance->date = date('Y',time());
                $performance->save();
                $system = SystemSetting::where('app','ceping')->get();
                foreach ($system as $s){
                    $feed = new Feedback();
                    $feed->user_id = $user->id;
                    $feed->app = $s->id;
                    $feed->score = 0;
                    $feed->save();
                }
            }
            return Response::json(array('id'=>$user->id,'status'=>'success','msg'=>'用户数据保存成功！','redirectUrl'=>'/admin/user'));
        }


    }



    public function deleteUserDelete($id)
    {
        if($id <= 1)
            return  Response::json(array('status'=>'error','msg'=>'删除失败'));
        $folder = User::find($id);

        if ($folder->delete())
            return  Response::json(array('status'=>'success','msg'=>'用户删除成功!'));
        else
            return  Response::json(array('status'=>'error','msg'=>'删除失败'));
    }

}


